<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>

<body>
    <div class="layuimini-container">
        <div class="layuimini-main">

            <fieldset class="table-search-fieldset">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <form class="layui-form layui-form-pane" action="">
                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">全名搜索</label>
                                <div class="layui-input-inline">
                                    <input type="text" name="fullName" autocomplete="off" class="layui-input">
                                </div>
                            </div>
                            <div class="layui-inline">
                                <button type="submit" class="layui-btn layui-btn-primary" lay-submit
                                    lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            </div>
                        </div>
                    </form>
                </div>
            </fieldset>

            <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除 </button>
            </div>
        </script>

            <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

            <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

        </div>
    </div>
    <script src="../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form', 'table'], function () {
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table;

            table.render({
                elem: '#currentTableId',
                url: 'http://localhost:8080/api/employee?operation=getEmployeesByPage',
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [
                    [
                        { type: "checkbox", width: 50 },
                        { field: 'employeeId', width: 80, title: 'ID', sort: true },
                        {
                            title: '头像',
                            width: 80,
                            templet: function (d) {
                                return '<img src="' + d.avatar + '" alt="头像" class="layui-nav-img" >';
                            }
                        },
                        { field: 'fullName', width: 100, title: '全名' },
                        { field: 'email', width: 150, title: '电子邮件' },
                        { field: 'phone', width: 120, title: '电话号码' },
                        { field: 'department', width: 100, title: '部门' },
                        { field: 'position', width: 100, title: '职位' },
                        {
                            field: 'joinDate',
                            width: 160, // 增加宽度以容纳更多字符
                            title: '入职日期',
                            sort: true,
                            templet: function (d) {
                                return layui.util.toDateString(d.joinDate, 'yyyy-MM-dd HH:mm:ss');
                            }
                        },
                        {
                            field: 'salary',
                            width: 100,
                            title: '薪资',
                            sort: true,
                            templet: function (d) {
                                return d.salary + '$';
                            }
                        },
                        { title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center" }
                    ]
                ],
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {
                //执行搜索重载
                table.reload('currentTableId', {
                    page: {
                        curr: 1
                    }
                    , where: {
                        searchParams: data.field.fullName
                    }
                }, 'data');

                return false;
            });

            /**
             * toolbar监听事件
             */
            table.on('toolbar(currentTableFilter)', function (obj) {
                if (obj.event === 'add') {  // 监听添加操作
                    var index = layer.open({
                        title: '添加',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table/add-employee.html',
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                } else if (obj.event === 'delete') {  // 监听删除操作
                    var checkStatus = table.checkStatus('currentTableId')
                        , data = checkStatus.data;

                    if (data.length === 0) {
                        layer.msg('请选择要删除的数据', { icon: 2 });
                        return;
                    }

                    layer.confirm('确定要删除选中的数据吗？', { icon: 3, title: '确认删除' }, function (index) {
                        // 关闭确认框
                        layer.close(index);

                        var ids = data.map(function (item) {
                            return item.employeeId; // 假设ID字段名为id
                        });

                        // 执行批量删除操作
                        $.ajax({
                            type: 'POST',
                            url: 'http://localhost:8080/api/employee?operation=deleteBatch', // 替换为批量删除API的URL
                            contentType: 'application/json', // 设置请求的Content-Type为JSON
                            data: JSON.stringify(ids), // 向后端发送ID集合
                            success: function (result) {
                                if (result.code == 0) {
                                    layui.table.reload('currentTableId'); // 通过表格的 id 来重新加载数据
                                    // 批量删除成功，提示用户
                                    layer.msg('批量删除成功', { icon: 1 });
                                    // 刷新表格或其他操作
                                } else {
                                    // 批量删除失败，提示用户
                                    layer.msg('批量删除失败', { icon: 2 });
                                }
                            },
                            error: function () {
                                // 请求失败，提示用户
                                layer.msg('请求失败', { icon: 2 });
                            }
                        });
                    });
                }
            });

            //监听表格复选框选择
            table.on('checkbox(currentTableFilter)', function (obj) {
                console.log(obj)
            });

            table.on('tool(currentTableFilter)', function (obj) {
                var data = obj.data;
                var id = data.employeeId; // 获取ID
                if (obj.event === 'edit') {
                    var index = layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        shadeClose: true,
                        area: ['100%', '100%'],
                        content: '../page/table/edit-employee.html',
                        // 关键处、传参
                        success: function (layero, index) {
                            var iframeWin = window[layero.find('iframe')[0]['name']];
                            //表单数据回显,重点下面这句，可以将弹出的form对象获得
                            iframeWin.layui.form.getFormVal(JSON.stringify(data));
                        }
                    });
                    $(window).on("resize", function () {
                        layer.full(index);
                    });
                    return false;
                } else if (obj.event === 'delete') {
                    // 弹出确认框
                    layer.confirm('真的删除行么', function (index) {
                        console.log(id)
                        // 模拟删除请求，替换成实际的后端删除操作
                        $.ajax({
                            type: 'DELETE',
                            url: 'http://localhost:8080/api/employee?operation=delete&id=' + id, // 删除API的URL
                            contentType: 'application/json', // 设置请求的Content-Type为JSON
                            success: function (result) {
                                if (result.code == 0) {
                                    layui.table.reload('currentTableId'); // 通过表格的 id 来重新加载数据
                                    // 删除成功，提示用户
                                    layer.msg('删除成功', { icon: 1 });
                                    // 删除表格中的数据行
                                    obj.del();
                                    // 最后关闭确认框
                                    layer.close(index);
                                } else {
                                    // 删除失败，提示用户
                                    layer.msg('删除失败', { icon: 2 });
                                }
                            },
                            error: function () {
                                // 请求失败，提示用户
                                layer.msg('请求失败', { icon: 2 });
                            }
                        });
                    });

                }
            });

        });
    </script>

</body>

</html>